#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
    vector<vector<int>> ans;
    vector<int> slice;
    inline bool isleaf(TreeNode *n) { return n != NULL && n->left == NULL && n->right == NULL; }
    void dfs(TreeNode *root, int sum)
    {
        if (root == NULL)
            return;
        slice.push_back(root->val);
        sum -= root->val;
        if (isleaf(root) && sum == 0)
            ans.push_back(slice);
        dfs(root->left, sum);
        dfs(root->right, sum);
        slice.pop_back();
    }
    vector<vector<int>> pathSum(TreeNode *root, int sum)
    {
        dfs(root);
        return ans;
    }
};